<?php

namespace App\Http\Controllers\Admin\Superadmin;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\AdminLoginPostRequest;

class LoginController extends Controller
{
   /*
		显示后台登录页面
   */
    public function getLogin(){
    	//显示模板
    	return view('admin.superadmin.login.login');
    }
 
	/*
		后台管理员及作者验证
	*/
	public function postCheck(AdminLoginPostRequest $request){
		//检测用户名是否存在
		//$data = $request->input('make');
		
		$user = DB::table('hs_member')->where('name',$request->input('username'))->first();
		$data = $user->make;
		$state = $user->state;
		
		if($state != 2){



			// 检测密码是否一致
				if(Hash::check($pass = $request->input('password'),$user->password)){
					
					// 登录成功  将id存入session  
					session(['id'=>$user->id]);
					//跳转到后台首页
					if($data == 2){
							return  redirect('/admin')->with('success','欢迎'.$user->name.'登录');
						}else if($data == 1){
							return  redirect('/admin/author')->with('success','欢迎'.$user->name.'登录');
					}else{
						return '请前台登录';
					}
					// return back();
				
			}else{
				return back()->with('error','用户名或密码错误');
			}



		}else{
				return back()->with('error','您已被屏蔽');

		}
		
	}

	//编辑登录验证
	public function postChecke(AdminLoginPostRequest $request){
		//检测用户名是否存在
		$user = DB::table('hs_editor')->where('e_name',$request->input('username'))->first();
		// dd($user);
		if($user){

			// 检测密码是否一致
			if(Hash::check($pass = $request->input('password'),$user->password)){
					
					// 登录成功  将id存入session  
					session(['id'=>$user->id]);
					//跳转到后台编辑首页
					return  redirect('/admin/editor')->with('success','欢迎'.$user->e_name.'登录');
					
				
			}else{
				return back()->with('error','用户名或密码错误');
			}



		}else{
				return back()->with('error','用户名不存在');

		}
		// return 'qefwger';
		
	}

	public function getLogine(){
    	//显示模板
    	return view('admin.superadmin.login.logine');
    }

	public function getLogout(Request $request)
	{
		$request->session()->forget('id');
		return back();
	}	
}	
